package net.oschina.bilbodai.common.retrofitor.def;

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;

import java.util.concurrent.TimeUnit;

/**
 * @author daimao  Date: 16/4/15 Time: 上午11:02
 * @version $Id$
 */
public class DefaultOkHttpClientFactory {
    private int connectTimeout = 10_000;
    private int readTimeout = 10_000;
    private int writeTimeout = 10_000;
    private boolean followSslRedirects = true;
    private boolean followRedirects = true;
    private boolean retryOnConnectionFailure = true;
    private int maxIdleConnections = 5;
    private long keepAliveMills = 5 * 60 * 1000; //5min

    public OkHttpClient create() {
        return new OkHttpClient.Builder()
                .connectTimeout(getConnectTimeout(), TimeUnit.MILLISECONDS)
                .readTimeout(getReadTimeout(), TimeUnit.MILLISECONDS)
                .writeTimeout(getWriteTimeout(), TimeUnit.MILLISECONDS)
                .followRedirects(isFollowRedirects())
                .followSslRedirects(isFollowSslRedirects())
                .retryOnConnectionFailure(isRetryOnConnectionFailure())
                .connectionPool(new ConnectionPool(getMaxIdleConnections(), getKeepAliveMills(), TimeUnit.MILLISECONDS))
                .build();
    }

    public boolean isFollowSslRedirects() {
        return followSslRedirects;
    }

    public void setFollowSslRedirects(boolean followSslRedirects) {
        this.followSslRedirects = followSslRedirects;
    }

    public boolean isFollowRedirects() {
        return followRedirects;
    }

    public void setFollowRedirects(boolean followRedirects) {
        this.followRedirects = followRedirects;
    }

    public boolean isRetryOnConnectionFailure() {
        return retryOnConnectionFailure;
    }

    public void setRetryOnConnectionFailure(boolean retryOnConnectionFailure) {
        this.retryOnConnectionFailure = retryOnConnectionFailure;
    }

    public int getMaxIdleConnections() {
        return maxIdleConnections;
    }

    public void setMaxIdleConnections(int maxIdleConnections) {
        this.maxIdleConnections = maxIdleConnections;
    }

    public long getKeepAliveMills() {
        return keepAliveMills;
    }

    public void setKeepAliveMills(long keepAliveMills) {
        this.keepAliveMills = keepAliveMills;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public int getWriteTimeout() {
        return writeTimeout;
    }

    public void setWriteTimeout(int writeTimeout) {
        this.writeTimeout = writeTimeout;
    }
}